package com.example.testapp.activity.activity.read;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.example.testapp.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by l on 2016/7/31.
 */
public class NotifyTestActivity extends AppCompatActivity {

    private List<String>  mData;
    private ListView  mListView;
    private NotifyAdapter  mAdapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.view_notify);
        mData = new ArrayList<>();
        for (int i = 0; i < 20; i++){
            mData.add("" + i);
        }
        mListView = (ListView)findViewById(R.id.listView);
        mAdapter = new NotifyAdapter(this, mData);
        mListView.setAdapter(mAdapter);
        for (int i = 0; i < mListView.getChildCount(); i++){
            View view = mListView.getChildAt(i);
        }

        findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mData.add("new");
                mAdapter.notifyDataSetChanged();
                mListView.setSelection(mData.size()-1);
            }
        });
    }

    public class NotifyAdapter extends BaseAdapter{

        private List<String> mData;
        private LayoutInflater mInflater;

        public NotifyAdapter(Context context, List<String> data) {
            this.mData = data;
            mInflater = LayoutInflater.from(context);
        }

        @Override
        public int getCount() {
            return mData.size();
        }

        @Override
        public Object getItem(int position) {
            return mData.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder = null;
            if(convertView == null){
                holder = new ViewHolder();
                convertView = mInflater.inflate(R.layout.view_notify_item, null);
                holder.imageView = (ImageView)convertView.findViewById(R.id.imageView);
                holder.textView = (TextView)convertView.findViewById(R.id.textView);
                convertView.setTag(holder);
            }else{
                holder = (ViewHolder) convertView.getTag();
            }
            holder.imageView.setBackgroundResource(R.mipmap.ic_launcher);
            holder.textView.setText(mData.get(position));
            return convertView;
        }

        public class ViewHolder{
            ImageView imageView;
            TextView  textView;
        }
    }

}
